<?php

namespace App\Admin\Controllers;

use App\Models\Resource;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ResourceController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

        return Admin::content(function (Content $content) {
            $content->header('其他分类');
//            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('其他分类');
//            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('其他分类');
//            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::script("popverLoad();");
        return Admin::grid(Resource::class, function (Grid $grid) {
            $grid->disableExport();

            $grid->id('ID')->sortable();
            $grid->name('分类名称');
            $grid->column('icon_url', '分类图标')->display(function ($value) {
                $url = asset('uploads/' . $value);

                return '
                    <a class="resouce-popver layui-btn layui-btn-primary" href="javascript:;" src-data="' . $url . '">预览</a>
                ';
            });

            $states = [
                'on'  => ['value' => 'enabled', 'text' => '启用', 'color' => 'primary'],
                'off' => ['value' => 'disabled', 'text' => '禁用', 'color' => 'danger'],
            ];
            $grid->status('状态')->switch($states);

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like('name', '分类名称');
                $filter->equal('status', '状态')->radio([
                    ''   => 'All',
                    'enabled' => '启用',
                    'disabled' => '禁用',
                ]);
                $filter->between('created_at', '添加时间')->datetime();
            });

            $grid->created_at('添加时间');
//            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Resource::class, function (Form $form) {

//            $form->display('id', 'ID');

            $form->text('name', '分类名称')->rules('required|max:60');

            $form->image('icon_url', '图标')
                ->help('图标的大小不能超过1MB')
                ->uniqueName()
                ->removable()
                ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                ->rules('required|image|max:1024');

            $states = [
                'on'  => ['value' => 'enabled', 'text' => '启用', 'color' => 'primary'],
                'off' => ['value' => 'disabled', 'text' => '禁用', 'color' => 'danger'],
            ];
            $form->switch('status', '状态')->states($states)->default('on');

            $form->hidden('admin_id')->value(Admin::user()->id);
//            $form->display('created_at', '添加时间');
//            $form->display('updated_at', '更新时间');
        });
    }
}
